package lining.ruangong.wanli.calculator;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private TextView resultTextView, processTextView;
    private Button numButton0, numButton1, numButton2, numButton3, numButton4,
            numButton5, numButton6, numButton7, numButton8, numButton9,
            clearButton, deleteButton,reaganButton, remainButton, addButton, reduceButton,
            multiplyButton, divisionButton, decimalButton, equalButton;
    // 按钮监听对象
    private ButtonListener buttonListener;
    // TextView显示的文本
    private String resultString = "0";
    // 记录需要计算的数字
    private String numString1 = "";
    private String numString2 = "";
    // 记录运算符号
    private String symbol = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 实例化按钮监听对象
        buttonListener = new ButtonListener();

        // 判断当前屏幕的方向
        if(this.getResources().getConfiguration().orientation== Configuration.ORIENTATION_LANDSCAPE)
        {
            // 横屏
            setContentView(R.layout.layout_land);

            // 获取横屏布局控件
            setupUIWithHorizontalLayout();

            // 设置按钮监听
            setupListenerWithHorizontalLayout();
        }
        else if(this.getResources().getConfiguration().orientation==Configuration.ORIENTATION_PORTRAIT)
        {
            // 竖屏
            setContentView(R.layout.activity_main);

            // 获取竖屏布局控件
            setupUIWithVerticalLayout();

            // 设置按钮监听
            setupListenerWithVerticalLayout();
        }
    }

    // 设置横屏布局的按钮监听
    private void setupListenerWithHorizontalLayout() {

        numButton0.setOnClickListener(buttonListener);
        numButton1.setOnClickListener(buttonListener);
        numButton2.setOnClickListener(buttonListener);
        numButton3.setOnClickListener(buttonListener);
        numButton4.setOnClickListener(buttonListener);
        numButton5.setOnClickListener(buttonListener);
        numButton6.setOnClickListener(buttonListener);
        numButton7.setOnClickListener(buttonListener);
        numButton8.setOnClickListener(buttonListener);
        numButton9.setOnClickListener(buttonListener);
        clearButton.setOnClickListener(buttonListener);
        deleteButton.setOnClickListener(buttonListener);
        remainButton.setOnClickListener(buttonListener);
        addButton.setOnClickListener(buttonListener);
        reduceButton.setOnClickListener(buttonListener);
        multiplyButton.setOnClickListener(buttonListener);
        divisionButton.setOnClickListener(buttonListener);
        decimalButton.setOnClickListener(buttonListener);
        equalButton.setOnClickListener(buttonListener);
        reaganButton.setOnClickListener(buttonListener);
    }

    // 获取竖屏布局的控件
    private void setupUIWithHorizontalLayout() {

        resultTextView = (TextView) findViewById(R.id.v_tv_result);
        processTextView = (TextView) findViewById(R.id.v_tv_process);
        numButton0 = (Button) findViewById(R.id.v_btn_0);
        numButton1 = (Button) findViewById(R.id.v_btn_1);
        numButton2 = (Button) findViewById(R.id.v_btn_2);
        numButton3 = (Button) findViewById(R.id.v_btn_3);
        numButton4 = (Button) findViewById(R.id.v_btn_4);
        numButton5 = (Button) findViewById(R.id.v_btn_5);
        numButton6 = (Button) findViewById(R.id.v_btn_6);
        numButton7 = (Button) findViewById(R.id.v_btn_7);
        numButton8 = (Button) findViewById(R.id.v_btn_8);
        numButton9 = (Button) findViewById(R.id.v_btn_9);
        // C
        clearButton = (Button) findViewById(R.id.v_btn_clear);
        // 退格
        deleteButton = (Button) findViewById(R.id.v_btn_delete);
        // √
        reaganButton = (Button) findViewById(R.id.v_btn_reagan);
        // %
        remainButton = (Button) findViewById(R.id.v_btn_remain);
        // +
        addButton = (Button) findViewById(R.id.v_btn_add);
        // -
        reduceButton = (Button) findViewById(R.id.v_btn_reduce);
        // *
        multiplyButton = (Button) findViewById(R.id.v_btn_multiply);
        // ÷
        divisionButton = (Button) findViewById(R.id.v_btn_division);
        // '.'
        decimalButton = (Button) findViewById(R.id.v_btn_decimal);
        // '='
        equalButton = (Button) findViewById(R.id.v_btn_equal);
    }






    // 设置竖屏布局的按钮监听
    private void setupListenerWithVerticalLayout() {

        numButton0.setOnClickListener(buttonListener);
        numButton1.setOnClickListener(buttonListener);
        numButton2.setOnClickListener(buttonListener);
        numButton3.setOnClickListener(buttonListener);
        numButton4.setOnClickListener(buttonListener);
        numButton5.setOnClickListener(buttonListener);
        numButton6.setOnClickListener(buttonListener);
        numButton7.setOnClickListener(buttonListener);
        numButton8.setOnClickListener(buttonListener);
        numButton9.setOnClickListener(buttonListener);
        clearButton.setOnClickListener(buttonListener);
        deleteButton.setOnClickListener(buttonListener);
        remainButton.setOnClickListener(buttonListener);
        addButton.setOnClickListener(buttonListener);
        reduceButton.setOnClickListener(buttonListener);
        multiplyButton.setOnClickListener(buttonListener);
        divisionButton.setOnClickListener(buttonListener);
        decimalButton.setOnClickListener(buttonListener);
        equalButton.setOnClickListener(buttonListener);
    }


    // 获取竖屏布局的控件
    private void setupUIWithVerticalLayout() {
        resultTextView = (TextView) findViewById(R.id.tv_result);
        processTextView = (TextView) findViewById(R.id.tv_process);
        numButton0 = (Button) findViewById(R.id.btn_0);
        numButton1 = (Button) findViewById(R.id.btn_1);
        numButton2 = (Button) findViewById(R.id.btn_2);
        numButton3 = (Button) findViewById(R.id.btn_3);
        numButton4 = (Button) findViewById(R.id.btn_4);
        numButton5 = (Button) findViewById(R.id.btn_5);
        numButton6 = (Button) findViewById(R.id.btn_6);
        numButton7 = (Button) findViewById(R.id.btn_7);
        numButton8 = (Button) findViewById(R.id.btn_8);
        numButton9 = (Button) findViewById(R.id.btn_9);
        // C
        clearButton = (Button) findViewById(R.id.btn_clear);
        // 退格
        deleteButton = (Button) findViewById(R.id.btn_delete);
        // %
        remainButton = (Button) findViewById(R.id.btn_remain);
        // +
        addButton = (Button) findViewById(R.id.btn_add);
        // -
        reduceButton = (Button) findViewById(R.id.btn_reduce);
        // *
        multiplyButton = (Button) findViewById(R.id.btn_multiply);
        // ÷
        divisionButton = (Button) findViewById(R.id.btn_division);
        // '.'
        decimalButton = (Button) findViewById(R.id.btn_decimal);
        // '='
        equalButton = (Button) findViewById(R.id.btn_equal);
    }


    // 获取按钮的文本
    private String getButtonText(View v) {

        Button button = (Button)v;

        return button.getText().toString();
    }


    // 点击运算符号按钮,记录算式中的变量
    private void recodeProcess(View v) {

        // 判断算数中是否已有运算符号
        // 如果最后一位是运算符号, 直接返回
        if ((!symbol.isEmpty()) && resultString.endsWith(symbol)) {

            numString1 = "";
            numString2 = "";
            resultString = "0";
            symbol = "";
            processTextView.setText("");
            Toast.makeText(getApplicationContext(), "输入不合法!请重新输入", Toast.LENGTH_SHORT).show();
            return;
        }
        else if ((!symbol.isEmpty()) && resultString.contains(symbol)) {
            calcualteResult();
        }


        // 记录第一个数字
        numString1 = (resultString == "") ? "1" : resultString;

        resultString += getButtonText(v);
        // 记录运算符号
        symbol = getButtonText(v);
    }

    // 计算结果
    private void calcualteResult() {

        // 判断运算符号是否为空
        if (symbol.equals("") || resultString.endsWith(symbol)) {
            return;
        }

        // 记录当前的算式
        processTextView.setText(resultString);

        if (resultString.startsWith("√")) {

            // 记录第二个数字
            numString2 = resultString.substring(numString1.length(), resultString.length());
        }
        else {

            // 记录第二个数字
            numString2 = resultString.substring(numString1.length()+1, resultString.length());
        }

        // 判断两个数字是否为小数 / 当前运算的是否是除法
        if (numString1.contains(".") || numString2.contains(".") || symbol.equals("÷") || symbol.equals("√")) {

            if (symbol.equals("%")) {
                numString1 = "";
                numString2 = "";
                resultString = "0";
                processTextView.setText("");
                Toast.makeText(getApplicationContext(), "数字不合法!请重新输入", Toast.LENGTH_SHORT).show();
                return;
            }

            // 将字符串转换成数字
            float number1 = Float.parseFloat(numString1);
            float number2 = Float.parseFloat(numString2);

            // 判断运算符号
            switch (symbol) {
                case "+":
                    resultString = (number1 + number2) + "";
                    break;
                case "-":
                    resultString = (number1 - number2) + "";
                    break;
                case "*":
                    resultString = (number1 * number2) + "";
                    break;
                case "÷":
                    resultString = (number1 / number2) + "";
                    break;
                case "√":
                    resultString = number1 * Math.sqrt(number2) + "";
            }
        }
        else {
            int number1 = Integer.parseInt(numString1);
            int number2 = Integer.parseInt(numString2);

            // 判断运算符号
            switch (symbol) {
                case "+":
                    resultString = (number1 + number2) + "";
                    break;
                case "-":
                    resultString = (number1 - number2) + "";
                    break;
                case "*":
                    resultString = (number1 * number2) + "";
                    break;
                case "%":
                    resultString = (number1 % number2) + "";
                    break;
            }
        }
        // 清除运算符号
        symbol = "";
        numString1 = "";
        numString2 = "";

        // 更新 TextView
        resultTextView.setText(resultString);
    }


    // 创建内部类, 实现按钮的监听方法
        private class ButtonListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {

            switch (v.getId()) {

                // 清屏
                case R.id.btn_clear:
                    resultString = "0";
                    numString1 = "";
                    numString2 = "";
                    break;
                case R.id.v_btn_clear:
                    resultString = "0";
                    numString1 = "";
                    numString2 = "";
                    break;

                // 退格
                case R.id.btn_delete:
                    resultString = resultString.substring(0, resultString.length() - 1);
                    if (resultString.length() == 0) {

                        resultString = "0";
                    }
                    break;
                case R.id.v_btn_delete:
                    resultString = resultString.substring(0, resultString.length() - 1);
                    if (resultString.length() == 0) {

                        resultString = "0";
                    }
                    break;

                // √
                case R.id.v_btn_reagan:
                    if (resultString == "0") {
                        resultString = "";
                    }
                    recodeProcess(v);
                    break;

                // %
                case R.id.btn_remain:
                    recodeProcess(v);
                    break;
                case R.id.v_btn_remain:
                    recodeProcess(v);
                    break;

                // +
                case R.id.btn_add:
                    // 记录算式中的变量
                    recodeProcess(v);
                    break;
                case R.id.v_btn_add:
                    // 记录算式中的变量
                    recodeProcess(v);
                    break;

                // -
                case R.id.btn_reduce:
                    if (resultString.equals("0")) {
                        resultString = getButtonText(v);
                        break;
                    }
                    recodeProcess(v);
                    break;
                case R.id.v_btn_reduce:
                    if (resultString.equals("0")) {
                        resultString = getButtonText(v);
                        break;
                    }
                    recodeProcess(v);
                    break;

                // *
                case R.id.btn_multiply:
                    recodeProcess(v);
                    break;
                case R.id.v_btn_multiply:
                    recodeProcess(v);
                    break;

                // ÷
                case R.id.btn_division:
                    recodeProcess(v);
                    break;
                case R.id.v_btn_division:
                    recodeProcess(v);
                    break;

                // =, 计算结果
                case R.id.btn_equal:
                    calcualteResult();
                    break;
                case R.id.v_btn_equal:
                    calcualteResult();
                    break;

                // 点击的数字按钮
                default:
                    if (resultString.equals("0") && !getButtonText(v).equals(".")) {

                        resultString = getButtonText(v);
                    }
                    else {

                        resultString += getButtonText(v);
                    }
                    break;
            }

            // 更新 TextView
            resultTextView.setText(resultString);
        }
    }
}
